package com.mdnote.structure.stack;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Rhythm-2019
 * @version 1.0
 * @date 2020/8/24
 * @description 用于判断符号的优先级
 */
public class Operation {

        private static final Map<Character, Integer> OPERATION_PRIORITY_MAP = new HashMap<>();

    static {
        OPERATION_PRIORITY_MAP.put('+', 1);
        OPERATION_PRIORITY_MAP.put('-', 1);
        OPERATION_PRIORITY_MAP.put('*', 2);
        OPERATION_PRIORITY_MAP.put('/', 2);
    }

    /**
     * 判断符号优先级
     * @param o1 操作符号1
     * @param o2 操作符号2
     * @return 操作符号1优先级是否高于炒作符号2
     */
    public static boolean comparePriority(char o1, char o2) {
        Integer p1 = OPERATION_PRIORITY_MAP.get(o1);
        Integer p2 = OPERATION_PRIORITY_MAP.get(o2);
        if (p1 == null || p2 == null) {
            throw new IllegalArgumentException("operation can not be found");
        }
        return p1 >= p2;
    }

    /**、
     * 计算值
     * @param n1 操作数
     * @param n2 被操作数
     * @param operation 操作符
     * @return 结果
     */
    public static Integer calculate(Integer n1, Integer n2, Character operation) {

        int rtn = 0;
        switch (operation) {
            case '+':
                rtn = n1 + n2;
                break;
            case '-':
                rtn = n1 - n2;
                break;
            case '*':
                rtn = n1 * n2;
                break;
            case '/':
                rtn = n1 / n2;
                break;
            default:
                throw new IllegalArgumentException("operation is error");
        }
        return rtn;
    }


}
